package org.lex.dbq.view.ddl;

import java.sql.Connection;
import java.sql.SQLException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.lex.dbq.model.jdbc.meta.VoConverter;

public class EditTableDialog extends Dialog {

	public static void show(Shell shell, Connection conn, String catalog, String schema, String table) {
		EditTableDialog d = new EditTableDialog(shell);
		d.conn = conn;
		d.catalog = catalog;
		d.schema = schema;
		d.table = table;

		d.open();
		d.close();
	}

	private Connection conn = null;
	private String catalog = null;
	private String schema = null;
	private String table = null;

	public EditTableDialog(Shell shell) {
		super(shell);
		this.setShellStyle(SWT.MAX | SWT.RESIZE);
	}

	@Override
	protected void cancelPressed() {
		// TODO Auto-generated method stub
		super.cancelPressed();
	}

	@Override
	protected void okPressed() {
		// TODO Auto-generated method stub
		super.okPressed();
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Edit table");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		TabFolder folder = new TabFolder(parent, SWT.NONE);
		folder.setLayoutData(new GridData(GridData.FILL_BOTH));

		ColumnsPane cp = new ColumnsPane(folder, SWT.NONE);
		KeysPane kp = new KeysPane(folder, SWT.NONE);

		Object[][] controls = { { cp, "Columns" }, { kp, "Keys" } };
		for (Object[] o : controls) {
			TabItem item = new TabItem(folder, SWT.NONE);
			item.setControl((Control) o[0]);
			item.setText((String) o[1]);
		}
		VoConverter conv = new VoConverter();
		// columns
		try {
			cp.setInput(conv.getColumns(conn.getMetaData(), catalog, schema, table, null));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// keys
		try {
			kp.setInput(conv.getKeys(conn.getMetaData(), catalog, schema, table));
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return parent;
	}
}
